package containers

import (
	"com.yy.api/common/logger"
	"com.yy.api/common/pojo/dto"
	types "com.yy.api/common/pojo/models"
	cP "com.yy.api/common/pojo/param/container"
	imP "com.yy.api/common/pojo/param/image"
	"com.yy.api/server/config"
	"com.yy.api/server/utils"
)

var agentClient = config.GetAgentClient()

func PullImage(rowImageParam string) bool {
	if err := agentClient.PullImage(imP.AssemblePullImageParam(rowImageParam)); err != nil {
		logger.Default.Errorf("pull image %v error：%v", rowImageParam, err)
		return false
	}
	return true
}
func CreateContainer(rowImageParam string, portMappingsParam []types.PortMapping, gpuIds []int, name string, userId string) (string, bool) {
	var createContainerDto dto.CreateContainerDto
	displayId, ok := utils.GenerateDisplayID()
	if !ok {
		return "", false
	}
	param := cP.AssembleCreateContainerParam(rowImageParam, portMappingsParam, gpuIds, name, userId, displayId)
	err := agentClient.CreateContainer(param, &createContainerDto)
	if err != nil {
		logger.Default.Errorf("%v", err)
		return "", false
	}
	return createContainerDto.ID, true
}
func StartContainer(containerId string) bool {
	if err := agentClient.StartContainer(containerId); err != nil {
		return false
	}
	return true
}

func InspectContainer(containerId string) (dto.InspectContainerDataDto, bool) {
	var reslt dto.InspectContainerDataDto
	if err := agentClient.InspectContainer(containerId, &reslt); err != nil {
		return reslt, false
	}
	return reslt, true
}
func HasContainer(containerIdOrNameOpt string) (bool, bool) {
	logger.Default.Infof("HasContainer: %v", containerIdOrNameOpt)
	var reslt dto.HasContainerDto
	if err := agentClient.HasContainer(cP.AssembleHasContainerParam(containerIdOrNameOpt), &reslt); err != nil {
		logger.Default.Errorf("%v", err)
		return false, false
	}
	return bool(reslt), true
}
func ReleaseContainer(containerIdOrNameOpt string) (dto.ReleaseContainerDto, bool) {
	var reslt dto.ReleaseContainerDto
	if err := agentClient.ReleaseContainer(cP.AssembleReleaseContainerParam(containerIdOrNameOpt), &reslt); err != nil {
		logger.Default.Errorf("%v", err)
		return reslt, false
	}
	return reslt, true
}
func ContainerList(userId string, displayId string, status []string) (dto.ListContainersDto, bool) {
	return queryContainerList(userId, displayId, status, "", []int{}, []string{})
}
func HealthCheckContainer(containerIdOrNameOpt string) (dto.HealthCheckContainerDto, bool) {
	var reslt dto.HealthCheckContainerDto
	if err := agentClient.HealthCheckContainer(cP.AssembleHealthCheckContainerParam(containerIdOrNameOpt), &reslt); err != nil {
		logger.Default.Errorf("%v", err)
		return reslt, false
	}
	return reslt, true
}
func queryContainerList(userId string, displayId string, status []string, name string, gpuIds []int, hostPorts []string) (dto.ListContainersDto, bool) {
	var reslt dto.ListContainersDto
	options := cP.NewListContainerParam()
	if userId != "" {
		options.UserId = userId
	}
	if displayId != "" {
		options.DisplayId = displayId
	}
	if name != "" {
		options.Name = name
	}
	if len(status) > 0 {
		options.Status = status
	}
	if len(gpuIds) > 0 {
		options.GpuIds = gpuIds
	}
	if len(hostPorts) > 0 {
		options.HostPorts = hostPorts
	}

	if err := agentClient.ListContainers(*options, &reslt); err != nil {
		logger.Default.Errorf("%v", err)
		return reslt, false
	}
	return reslt, true
}
